#include "Bluetooth.h"

bool isAdvertising = true; //是否在广播
int userCount = 0; //连接用户数量
String wifi_account = ""; //wifi账户
String wifi_password = ""; //wifi密码
uint32_t cnt_value = 0;
void MyServerCallbacks::onConnect(BLEServer* pServer) { //连接回调函数
    userCount++;
    isAdvertising = false;
}
void MyServerCallbacks::onDisconnect(BLEServer* pServer) { //断开回调函数
    userCount--;
}
void WifiCharactercallbacks::onRead(BLECharacteristic* pCharacteristic) { // 客户端读取事件回调函数
    Serial.println("666事件-用户读取");
}
void WifiCharactercallbacks::onWrite(BLECharacteristic* pCharacteristic) { // 客户端写入事件回调函数
    std::string value = std::string(pCharacteristic->getValue());
    if (value[0] == 'a') //账户
    {
        wifi_account = "";
        for(int i = 1; i < value.length(); i++)
        {
            wifi_account += value[i]; 
        }
        Serial.println("777事件-设置账户" + wifi_account);
    }else if(value[0] == 'p') //密码
    {
        wifi_password = "";
        for(int i = 1; i < value.length(); i++)
        {
            wifi_password += value[i]; 
        }
        Serial.println("888事件-设置密码" + wifi_password);
    }
    if(wifi_account == "zlk" && wifi_password == "666666")
    {
        sendMassage("Success");
        Serial.println("配网成功！");
    }else{
        sendMassage("Fail！");
        Serial.println("配网失败！");
    }
}
void WifiCharactercallbacks::onNotify(BLECharacteristic* pCharacteristic) { // 客户端读取事件回调函数
    Serial.println("555事件-通知用户");
}
BLEServer *pServer = NULL;//创建一个服务器
BLEService *pService = NULL;//创建一个服务
BLECharacteristic *pCharacter_wifiConfig = NULL;//创建一个特征
BLEAdvertising *pAdvertising = NULL; //创建一个广播
void BLE_init()
{
    BLEDevice::init(BLE_NAME);//初始化蓝牙设备
    pServer = BLEDevice::createServer();//创建一个服务器
    pServer->setCallbacks(new MyServerCallbacks);//创建回调函数
    pService = pServer->createService(service_uuid);//创建一个服务
    /*  创建一个特征  */
    pCharacter_wifiConfig = pService->createCharacteristic(charact_uuid, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE | BLECharacteristic::PROPERTY_NOTIFY);
    pCharacter_wifiConfig->setCallbacks(new WifiCharactercallbacks()); //创建回调函数
    pCharacter_wifiConfig->setValue("Wifi Config"); //给特征值赋值
    pCharacter_wifiConfig->addDescriptor(new BLE2902());//启用Notify的都需要加这个
    //开始服务
    pService->start();
    pAdvertising = BLEDevice::getAdvertising();//获取广播
    /*  将Service加入广播  */
    pAdvertising->addServiceUUID(service_uuid);
    pAdvertising->setScanResponse(true); //允许扫描回复
    pAdvertising->setMinPreferred(0x12);
    BLEDevice::startAdvertising();//开始广播
}

void judgeBleAdvertising()
{
    if(BLEDevice::getInitialized() && !isAdvertising && userCount < MAX_USERCOUNT)
    {
      delay(500);
      BLEDevice::startAdvertising();
      isAdvertising = true;
    }
}
void sendMassage(std::string str)
{
    if(pCharacter_wifiConfig && userCount > 0)
    {
        pCharacter_wifiConfig->setValue(str);
        pCharacter_wifiConfig->notify();
        Serial.println("999事件-发送信息到客户端！");
    }
}
/*             使用教程
// 蓝牙扫描 可实现断开后再次连接
void xTaskTree(void* xTask3)
{
  while(1)
  {
    judgeBleAdvertising();
    vTaskDelay(50 / portTICK_PERIOD_MS);
  }
}
*/